package com.ljxt.dt.web.servlet.basicinformationservlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljxt.dt.domain.GoodsInfo;
import com.ljxt.dt.service.basicinformationservice.SupplierListService;
import com.ljxt.dt.service.basicinformationservice.impl.SupplierListServiceImpl;
import com.ljxt.dt.vo.JsonResult;
import com.ljxt.dt.vo.PageVO;
import com.ljxt.dt.vo.basicInformation.GoodsInfoVO;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

/**
 * @author 糖糖
 * @version 1.0.0
 * @date 2020/5/14 20:30
 * @copyright 老九学堂
 */
@WebServlet("/addGoodsServlet")
public class AddGoodsServlet extends HttpServlet {

    private SupplierListService supplierListService = new SupplierListServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //List<GoodsInfo> goodsInfoList = supplierListService.findAllGoods();
       // req.setAttribute("goodList",goodsInfoList);
        req.getRequestDispatcher(req.getContextPath() + "WEB-INF/views/basicinformation/addGoodsModal.jsp").forward(req, resp);

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取模态框传入的数据
        String idString = req.getParameter("id");
        String goodsName = req.getParameter("goodsName");

        //获取分页参数
        String currentIndexString = req.getParameter("currentIndex");
        String pageSizeString = req.getParameter("pageSize");

        //1.判断分页参数的初始值是否为空，若为空，赋默认值
        if(currentIndexString == null){
            currentIndexString = "1";
        }
        if(pageSizeString == null){
            pageSizeString = "5";
        }

        //将String类型的分页参数转换为int类型
        int currentIndex = Integer.parseInt(currentIndexString);
        int pageSize = Integer.parseInt(pageSizeString);

        //判断传入的数据是否是数字类型的字符串
        boolean canCast = idString != null && idString.trim().length() > 0 && idString.trim().matches("^[1-9]+$");
       //如果是数字类型的字符串，则转换成int类型，否则赋值为-1
        int id = Integer.parseInt(canCast ? idString.trim() : "-1");
        if(goodsName == null){
            System.out.println("goodName的值为空");
            goodsName = "";
        }
        //调用方法查找指定的所有的商品信息集合
        List<GoodsInfoVO> goodsInfoVOList =  supplierListService.findALLGoods(id, goodsName, currentIndex, pageSize);
        //调用方法查找指定的所有的商品信息集合总条数
        int total =  supplierListService.findALLGoodsCount(id, goodsName, currentIndex, pageSize);
        //将商品信息集合放到pageVO中
        PageVO goodsListPageVO = new PageVO(goodsInfoVOList, currentIndex, pageSize, total);
        //将goodsListPageVO加到jsonResult中
        JsonResult<PageVO<GoodsInfoVO>> jsonResult = new JsonResult<>();
        jsonResult.setData(goodsListPageVO);
        //将json数据发送出去
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(resp.getWriter(), jsonResult);
    }
}
